﻿using System.Web.Mvc;
using AllIsWell.Core.Models;
using AllIsWell.Services.Infrastructure;

namespace AllIsWell.Web
{
    public class PageModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var page = base.BindModel(controllerContext, bindingContext) as Page;
            
            if (page == null)
            {
                return new Page();
            }

            var destinyService = DependencyResolver.Current.GetService<IDestinyService>();
            var whatService = DependencyResolver.Current.GetService<IWhatService>();
            
            page.Destiny = destinyService.GetDestiny(page.Destiny.Id);

            foreach (var retrospect in page.Retrospects)
            {
                retrospect.What = whatService.GetWhat(retrospect.What.Id);
            }

            return page;
        }
    }
}